package com.ruoyi.ticket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ticket.domain.bo.TicketGateRecordBo;
import com.ruoyi.ticket.domain.vo.TicketGateRecordVo;
import com.ruoyi.ticket.domain.TicketGateRecord;
import com.ruoyi.ticket.mapper.TicketGateRecordMapper;
import com.ruoyi.ticket.service.ITicketGateRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 闸机记录Service业务层处理
 *
 * @author xupeng
 * @date 2023-03-29
 */
@RequiredArgsConstructor
@Service
public class TicketGateRecordServiceImpl implements ITicketGateRecordService {

    private final TicketGateRecordMapper baseMapper;

    /**
     * 查询闸机记录
     */
    @Override
    public TicketGateRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询闸机记录列表
     */
    @Override
    public TableDataInfo<TicketGateRecordVo> queryPageList(TicketGateRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TicketGateRecord> lqw = buildQueryWrapper(bo);
        Page<TicketGateRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询闸机记录列表
     */
    @Override
    public List<TicketGateRecordVo> queryList(TicketGateRecordBo bo) {
        LambdaQueryWrapper<TicketGateRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TicketGateRecord> buildQueryWrapper(TicketGateRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TicketGateRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTicketNum()), TicketGateRecord::getTicketNum, bo.getTicketNum());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), TicketGateRecord::getDeviceId, bo.getDeviceId());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            TicketGateRecord::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(bo.getType() != null, TicketGateRecord::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增闸机记录
     */
    @Override
    public Boolean insertByBo(TicketGateRecordBo bo) {
        TicketGateRecord add = BeanUtil.toBean(bo, TicketGateRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改闸机记录
     */
    @Override
    public Boolean updateByBo(TicketGateRecordBo bo) {
        TicketGateRecord update = BeanUtil.toBean(bo, TicketGateRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TicketGateRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除闸机记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
